package test;

public class BinaryTree {
    static class TreeNode {
        private TreeNode left;
        private TreeNode right;
        private char val;
        public TreeNode(char val) {
            this.val = val;
        }
    }
    //根节点
    public TreeNode root;
    //创建一个二叉树
    public void createBinaryTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        this.root = A;
    }

    // 前序遍历
    void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }


    // 中序遍历
    void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        preOrder(root.left);
        System.out.print(root.val + " ");
        preOrder(root.right);
    }


    // 后序遍历
    void postOrde(TreeNode root) {
        if(root == null) {
            return;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + " ");
    }


    // 获取树中节点的个数(子问题思路)
    int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        //递归的求左右树的尺寸
        int left = size(root.left);
        int right = size(root.right);
        return left + right + 1;
    }
    //遍历思路
    public int noteSize;
    void size_1(TreeNode root) {
        if(root == null) {
            return;
        }
        noteSize++;
        size_1(root.left);
        size_1(root.right);
    }

    // 获取叶子节点的个数(遍历思路)
    public static int leafSize;
    void getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }


    // 子问题思路-求叶子结点个数
    int getLeafNodeCount1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        int left = getLeafNodeCount1(root.left);
        int right = getLeafNodeCount1(root.right);
        return left + right;
    }

    // 获取第K层节点的个数(子问题思路)
    int getKLevelNodeCount(TreeNode root, int key) {
        if(root == null) {
            return 0;
        }
        if(key == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, key - 1) +
                getKLevelNodeCount(root.right, key - 1);
    }


    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
    }
    //找结点
    TreeNode fund(TreeNode root, char val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        //就去找左树和右树
        TreeNode leftTree = fund(root.left, val);
        if(leftTree != null) return leftTree;
        TreeNode rightTree = fund(root.right, val);
        if(rightTree != null) return rightTree;
        return null;
    }

}
